package com.conan.atm;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;

/**
 * @version 加油！！！
 * @Description
 * @Author 谷藤新一GYC
 * @Date 2023/10/7-10:56
 */
public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();
    private Account loginAcc;
    Scanner scanner = new Scanner(System.in);
    /*
    启动ATM系统，展示欢迎界面
     */
    public void start(){
        System.out.println("===欢迎您进入到ATM系统===");
        System.out.println("1.用户登录");
        System.out.println("2.用户开户");
        int command = scanner.nextInt();
        switch (command){
            case 1:
                //用户登录
                login();
                break;
            case 2:
                //用户开户
                register();
                break;
            default:
                System.out.println("没有该项操作");
        }
    }
    /*
    登录功能
     */
    private void login(){
        System.out.println("===用户登录===");
        if (accounts.size()==0){
            System.out.println("当前系统中无账户，请先开户");
            return ;
        }
        while(true){
            System.out.print("请输入您的登录卡号：");
            String cardId = scanner.next();
            Account acc = getAccountByCardId(cardId);
            if (acc == null){
                System.out.println("您输入的卡号不正确，请重新输入。");
            }else {
                while(true){
                    System.out.print("请输入登陆密码：");
                    String passWord = scanner.next();
                    if(!passWord.equals(acc.getPassWord())){
                        System.out.println("您输入的密码错误，请重新输入");
                    }else {
                        loginAcc = acc;
                        System.out.println("恭喜您，"+acc.getUserName()+"成功登录系统，您的卡号是："+cardId);
                        //...
                        showUserCommand();
                        return;//跳出并退出登录页面
                    }
                }
            }
        }
    }

    /*
    显示登陆后的操作界面
     */
    private void showUserCommand(){
        while (true) {
            System.out.println("===您可以办理以下业务===");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            System.out.print("请选择：");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    showLoginAccount();
                    break;
                case 2:
                    saveMoney();
                    break;
                case 3:
                    withdrawMoney();
                    break;
                case 4:
                    transfer();
                    break;
                case 5:
                    changePassword();
                    break;
                case 6:
                    System.out.println(loginAcc.getUserName()+",您已退出登录！");
                    return;
                case 7:
                    deleteAccount();
                    return;
                default:
                    System.out.println("您输入的指令有误，请重新输入");

            }
        }
    }
    /*
    注销账户
     */
    private void deleteAccount(){
        System.out.print("是否确定注销账号(Y/N)：");
        char ok = scanner.next().charAt(0);
        if ('Y'==ok||'y'==ok){
            accounts.remove(loginAcc);
            System.out.println("成功注销账户！！！");
        }else if ('N'==ok||'n'==ok){
            System.out.println("取消注销账户！！！");
        }else {
            System.out.println("您输入的指令有误");
        }
    }

    /*
    修改密码
     */
    private void changePassword(){
        System.out.print("请输入原密码：");
        String ordPassword = scanner.next();
        if (!ordPassword.equals(loginAcc.getPassWord())){
            System.out.println("您输入的密码有误！！！");
        }else {
            while (true){
                System.out.print("请输入新密码：");
                String newPassword = scanner.next();
                System.out.print("请确认新密码：");
                String newDPassword = scanner.next();
                if (newPassword.equals(newDPassword)){
                    loginAcc.setPassWord(newPassword);
                    System.out.println("密码修改成功！！！");
                    return;
                }else {
                    System.out.println("您输入的两次不一样，请重新输入！！");
                }
            }

        }
    }

    /*
    转账功能
     */
    private void transfer(){
        System.out.print("请输入您要转账账户的卡号：");
        String tfCardId = scanner.next();
        Account tfAccount = getAccountByCardId(tfCardId);
        if(tfAccount==null){
            System.out.println("未找到该账户，请重新输入");
        }else {
            while (true){
                System.out.print("请输入转账金额：");
                double balance = scanner.nextDouble();
                if(balance<=0){
                    System.out.println("您输入的转账金额小于0，请重新输入！");
                }else if (balance>loginAcc.getLimit()){
                    System.out.println("您输入的转账金额超过上限，请重新输入！");
                } else {
                    loginAcc.setMoney((loginAcc.getMoney()-balance));
                    tfAccount.setMoney((tfAccount.getMoney()+balance));
                    return;
                }
            }

        }

    }

    /*
    存款功能
     */
    private void saveMoney(){
        System.out.print("请输入存款金额：");
        double balance = scanner.nextDouble();
        if(balance<=0){
            System.out.println("您要存的金额有误，请重新输入!");
        }else {
            double money = loginAcc.getMoney();
            money += balance;
            loginAcc.setMoney(money);
        }

    }

    /*
    取款功能
     */
    private void withdrawMoney(){

        System.out.print("请输入取款金额：");
        double balance = scanner.nextDouble();

        if(balance<=0){
            System.out.println("您输入的取款金额小于0，请重新输入！");
        }else if (balance>loginAcc.getLimit()){
            System.out.println("您输入的取款金额超过上限，请重新输入！");
        } else if (balance<loginAcc.getMoney()){
            System.out.println("余额不足！！");
        }else {
            double money = loginAcc.getMoney();
            money -= balance;
            loginAcc.setMoney(money);
        }
    }

    /*
    展示当前登陆用户信息
     */
    private void showLoginAccount(){
        System.out.println("当前您的账户信息如下：");
        System.out.println("卡号："+loginAcc.getCardId());
        System.out.println("姓名："+loginAcc.getUserName());
        System.out.println("性别："+loginAcc.getSex());
        System.out.println("余额："+loginAcc.getMoney());
        System.out.println("每次取现额度："+loginAcc.getLimit());
    }

    /*
    开户功能
     */
    private void register(){
        Account account = new Account();
        System.out.print("请输入姓名：");
        String userName = scanner.next();
        account.setUserName(userName);
        while(true){
            System.out.print("请输入性别：");
            char sex =  scanner.next().charAt(0);
            if (sex=='男'||sex=='女'){
                account.setSex(sex);
                break;
            }else {
                System.out.println("输入的性别有误,请重新输入");
            }
        }
        while(true){
            System.out.print("请输入您的账户密码：");
            String passWord = scanner.next();
            System.out.print("请再次输入密码：");
            String okPassWord = scanner.next();
            if(passWord.equals(okPassWord)){
                account.setPassWord(passWord);
                break;
            }else {
                System.out.println("您输入的两次密码不一样");
            }
        }
        System.out.print("请输入您的取款额度：");
        double limit = scanner.nextDouble();
        account.setLimit(limit);

        //重点：需要为这个账户生成一个卡号（由系统自动生成，8位数字表示，且不能重复）
        String replaceUUID = UUID.randomUUID().toString().replaceAll("[^0-9]", "").substring(0,8);
        account.setCardId(replaceUUID);
        accounts.add(account);
        System.out.println("恭喜你，"+account.getUserName()+"开户成功，您的卡号是："+account.getCardId());
    }

    /*
    根据卡号查询账户对象返回accounts = [c1,c2,c3,...]
     */
    private Account getAccountByCardId(String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)){
                return acc;
            }
        }
        return null;        //查无此人
    }

}
